#include "i2c.h"
#include "lcd03.h"


void I2C1_Init(void)
{
  lcd03Address = 0xc6;
}
// Remplace le handler par defaut
// les printf ralentissent fortement l'affichage -> style "minitel"...
void I2C1_IRQHandler(void)
{
    switch( LPC_I2C1->I2STAT  ) //-I2C interrupt cause/mode
    {
    	// MASTER TRANSMIT
    	//-START/REPEATED CONDITION TRANSMITTED
        case 0x08:
        case 0x10:
//printf("0x08 or 0x10 START/REPEATED START\n");
        /*
        //-Do a repeated start if requested (begins in case 18h|20h|40h|48h)
        //   if (!(I2cStatus & I2C_RESTART))
                i2c_no_start();
        //-transmit address
            I2C_DATA = *I2cDataPtr;
          */
        LPC_I2C1->I2CONCLR = 0x00000020;   ///< Keep a repeated start from happening
        LPC_I2C1->I2DAT = lcd03Address; // adresse esclave
        break;
//-SLAVE ADDRESS + WRITE TRANSMITTED - ACK RECEIVED
        case 0x18:
//printf("0x18 SLAVE ADDRESS + WRITE\n");
        //-Device has acknowledged that the master is talking to it so... send first 'data' byte
        // !!! Seulement pour LCD03 (le premier byte est toujours ignore par LCD03 !)
        // Sinon transmettre le premier byte
            LPC_I2C1->I2DAT = '?';
            //LPC_I2C1->I2DAT = *(data+dataPtr);
            //dataPtr++;
        break;
//-SLAVE ADDRESS + WRITE TRANSMITTED - NO ACK RECEIVED
        case 0x20:
//printf("0x20 SLAVE ADDRESS + WRITE NO ACK RECEIVED\n");
         //-Device is too busy to respond or is not present.
         //    I2cStatus = I2C_ERROR;     //-Transaction status
         I2C1_STOP_TX();//  LPC_I2C1->I2CONSET = 0x00000010;
        break;
//-DATA BYTE - ACK RECEIVED
        case 0x28:
//printf("0x28 SLAVE ADDRESS + WRITE NO ACK RECEIVED\n");
/*
            //-first data byte was received so send next... if more
            if (I2cNumByte > 0)
            {
                ++I2cDataPtr;
                I2C_DATA = *I2cDataPtr;    //-Send 'data byte' 2 through...
                --I2cNumByte;
            }
            else if (I2cStatus & I2C_RESTART)
            {
                I2cDataPtr -= I2cReStartIdx;  //-point to beginning (addr)
                *I2cDataPtr |= READ_I2C_DATA; //-Force read
                I2cNumByte = I2cNumRxByte;
                I2cStatus &= (~I2C_RESTART);  //-Clear flag
                i2c_start_tx();
            }//-send stop command since there are no more bytes to send
            else
            {
                i2c_stop_tx();
                I2cStatus = I2C_OK;
            }
*/
         if ( *(lcdData+dataPtr) != '\0' )
         {
          LPC_I2C1->I2DAT = *(lcdData+dataPtr);
          dataPtr++;
         }
         else
          I2C1_STOP_TX();  // LPC_I2C1->I2CONSET = 0x00000010;   // Sends a stop command
        break;
//-DATA BYTE TRANSMITTED - NO ACK RECEIVED
        case 0x30:
//printf("0x30 SLAVE ADDRESS + WRITE NO ACK RECEIVED\n");
/*
            i2c_stop_tx();
            I2cStatus = I2C_ERROR;
*/
        break;
//-Arbitration lost
        case 0x38:
            I2C1_STOP_TX();             //-Release bus
            // TODO Create auto-retry for ${x} number of times.
        break;
//-UNKNOWN STATE
        default:
            I2C1_STOP_TX();
            //I2cStatus = I2C_ERROR;
    }/*End Switch*/
    //////////////
	LPC_I2C1->I2CONCLR = 0x00000008; // i2c_clr_intrpt_flag();  //-clear the I2C interrupt flag.
}

